<?php
class Default_Model_Developer extends Zend_Db_Table{
	protected $_name = 'developer_files';
	protected $_primary = 'id';

	public function listGameType($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
		->from("gametype",array('id','name'))
		->order('id ASC')
		;
		$result = $db->fetchPairs($select);
		return $result ;
	}

	public function saveItem($arrParam = null, $options = null){
		if($options['task'] == 'developer-add'){
			$row =  $this->fetchNew();
			$row->name_app 	  	= $arrParam['name_app'];
			$row->developer_id 	= $arrParam['id'];
			$row->link_app 		= $arrParam['link_app'];
			$row->link_img 		= $arrParam['link_img'];
			
			$row->group_app 	= $arrParam['group_app'];
			$row->type_app		= $arrParam['type_app'] ;
			$row->star 			= 	0;
			$row->date_upload 	= date("Y-m-d");
			$row->des_app 		= $arrParam['des_app'];
			$row->status    	= 1 ;
			$row->number_download =  0 ;
			$row->save();
		}
		
		
		if($options['task'] == 'developer-edit'){
			$where = 'id = ' . $arrParam['id'];
				
			$row =  $this->fetchRow($where);
			$row->name_app 	  	= $arrParam['name_app'];
			$row->developer_id 	= 1;
			$row->link_app 		= $arrParam['link_app'];
			$row->link_img 		= $arrParam['link_img'];
			
			$row->group_app 	= $arrParam['group_app'];
			$row->type_app		= $arrParam['type_app'] ;
			$row->star 			= 	0;
			$row->date_upload 	= date("Y-m-d");
			$row->des_app 		= $arrParam['des_app'];
			$row->status    	= 1 ;
			$row->number_download =  0 ;
			$row->save();
		}

	}
	public function listItem($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);
		$paginator = $arrParam['paginator'];
		$ssFilter = $arrParam['ssFilter'];
		if($options['task'] == 'developer-list'){
			$select = $db->select()
			->from('developer_files AS d') 
			->where('d.developer_id=?',$arrParam['userId'])
			;
			
			if($paginator['itemCountPerPage']>0){
				$page = $paginator['currentPage'];
				$rowCount = $paginator['itemCountPerPage'];
				$select->limitPage($page,$rowCount);
			}
			if(!empty($ssFilter['keywords'])){
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('d.name_app LIKE ?',$keywords,"STRING");
			}
			if($ssFilter['group_id']>0){
				$select->where('d.group_app = ?',$ssFilter['group_id'],"INTEGER");
			}
			if(!empty($ssFilter['col']) && !empty($ssFilter['order'])){
				$select->order($ssFilter['col'] . ' ' . $ssFilter['order']);
			}
			
			$result  = $db->fetchAll($select);
		}
		
		if($options['task'] == 'admin-list-production'){
			$select = $db->select()
			->from('developer_files AS d')
			;
				
			if($paginator['itemCountPerPage']>0){
				$page = $paginator['currentPage'];
				$rowCount = $paginator['itemCountPerPage'];
				$select->limitPage($page,$rowCount);
			}
			if(!empty($ssFilter['keywords'])){
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('d.name_app LIKE ?',$keywords,"STRING");
			}
			if($ssFilter['group_id']>0){
				$select->where('d.group_app = ?',$ssFilter['group_id'],"INTEGER");
			}
			if(!empty($ssFilter['col']) && !empty($ssFilter['order'])){
				$select->order($ssFilter['col'] . ' ' . $ssFilter['order']);
			}
				
			$result  = $db->fetchAll($select);
		}
		return $result;
	}

	public function countItem($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);
		$ssFilter = $arrParam['ssFilter'];
		$select = $db->select()
		->from('developer_files AS d',array('COUNT(d.id) AS totalItem'));
		if($ssFilter['group_id']){
			$select->where('d.group_app=?',$ssFilter['group_id']);
		}
		//echo $select;
		$result = $db->fetchOne($select);
		return $result;

	}

	public function deleteItem($arrParam = null, $options = null){
		if($options['task'] == 'developer-delete'){
			$where = ' id = ' . $arrParam['id'];
			//----- LAY TEN HINH ANH user_avatar
			// 			$row = $this->getItem($arrParam,array('task'=>'delete')) ;
			// 			$imgName = $row['user_avatar'];
			// 			//------ XOA HINH ANH TREN SERVER
			// 			if(!empty($imgName)){
			// 				$upload_dir = FILES_PATH.'/users' ;
			// 				$upload = new Zendvn_File_Upload() ;
			// 				$upload->removeFile($upload_dir . '/orignal/' . $imgName) ;
			// 				$upload->removeFile($upload_dir . '/img100x100/' . $imgName) ;
			// 				$upload->removeFile($upload_dir . '/img450x450/' . $imgName) ;
			// 			}
			$this->delete($where);
		}

		if($options['task'] == 'developer-multi-delete'){
			$cid = $arrParam['cid'];

			if(count($cid)>0){
				$ids = implode(',',$cid);
				$where = 'id IN (' . $ids . ')';
				$this->delete($where);
			}
		}
	}
	public function getGroupApp(){
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
		->from('group_app', array('id','name'));
		$result = $db->fetchPairs($select)	;
		$result[0] = ' -- Select an Group -- ';
		ksort($result);
		return $result;
	}
	
	public function getTypeApp(){
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
		->from('type_app', array('id','name'));
		$result = $db->fetchPairs($select)	;
		$result[0] = ' -- Select Type -- ';
		ksort($result);
		return $result;
	}
	
	public function getItem($arrParam = null, $options = null){
	
		if($options['task'] == 'developer-info' || $options['task'] == 'developer-edit'){
			$db = Zend_Registry::get('connectDb');
			//$db = Zend_Db::factory($adapter,$config);
			$select = $db->select()
			->from('developer_files as d')
			->where('d.id = ?', $arrParam['id'], "INTEGER" );
				
			$result = $db->fetchRow($select);
		}
		//---- de lay ra ten file anh can delete
		if($options['task']=='delete'){
			$where = 'id='.$arrParam['id'];
			$result = $this->fetchRow($where)->toArray();
				
		}
		return $result;
	}
	
	
	public function changeStatus($arrParam = null, $options = null){
		$cid = $arrParam['cid'];
	
		if(count($cid)>0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}
				
			$ids = implode(',',$cid);
			$data = array('status'=>$status);
			$where = 'id IN (' . $ids . ')';
			$this->update($data,$where);
		}
		if($arrParam['id']>0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}
				
			$data = array('status'=>$status);
			$where = 'id = ' . $arrParam['id'];
			$this->update($data,$where);
		}
	
	}
	public function getAreaActive($id){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);
		$select = $db->select()
		->from('developer_files as d',array('COUNT(d.group_app)','d.group_app'))
		->where('d.developer_id = ?', $id, "INTEGER" )
		->group('group_app')
		;
		$result = $db->fetchAll($select);
		return $result;
	}

}